package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.PaperImageDao;
import com.leoman.entity.PaperImage;
import com.leoman.service.PaperImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * PaperImageServiceImpl
 * Created by 涂奕恒 on 2017/2/9 0009 14:47.
 */
@Service
public class PaperImageServiceImpl implements PaperImageService {

    @Autowired
    private PaperImageDao paperImageDao;

    @Override
    public List<PaperImage> findAll() {
        return paperImageDao.findAll();
    }

    @Override
    public Page<PaperImage> find(int pageNum, int pageSize) {
        return paperImageDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<PaperImage> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public PaperImage getById(int id) {
        return paperImageDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        paperImageDao.delete(id);
    }

    @Override
    public PaperImage create(PaperImage paperImage) {
        return paperImageDao.save(paperImage);
    }

    @Override
    public PaperImage update(PaperImage paperImage) {
        return paperImageDao.save(paperImage);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<PaperImage> findListByType(Integer type) {
        return paperImageDao.findListByType(type);
    }
}